<template>
  <div>
    <FormPanel ref="from" :list="formList" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <div style="height:47px;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)">保 存</multiuse-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getList, getDetail, postSave } from '@/api/omSale/vehicleParameters/capitalVerification';
export default {
  data() {
    this.saveData = null;
    return {
      formList: this.createdFrom()
    };
  },
  created() {
    console.log(this.formData);
    this.getDetail(this.formData.ruleId);
  },
  mixins: [dictionary],
  props: ['formData'],
  methods: {
    createdFrom() {
      return [
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008'),
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '百分比(%)',
          fieldName: 'percentage',

          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' },{
            validator:function(rule, value, callback) {
              if (value === undefined || value === '' || value === null) {
                callback();
              }
              var g = /^\-?[0-9]\d*.?\d*$/;
              if (g.test(value)) {
                if (100 >= value > 0) {
                  callback();
                }
              }
              callback(new Error('只能输入1-100的数字'));
            },trigger: 'blur'
          }]
        },
        {
          type: 'SELECT',
          label: '启用标记',
          fieldName: 'status',
          placeholder: '请选择',
          itemList: this.createDictList('1003'),
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'remark',
          placeholder: '请输入',
          selfCols: 3,
          maxlength: 50
        }
      ];
    },
    async getDetail(id) {
      let res = await getDetail({ ruleId: id });
      for (let i in res.data) {
        this.formList.map(x => {
          if (i == x.fieldName) {
            if (x.fieldName == 'powerType') {
              x.initialValue = res.data['powerType'] == 0 ? '' : res.data['powerType'];
            } else {
              x.initialValue = res.data[i];
            }
          }
        });
      }
    },
    changeHandle(val) {
      this.saveData = val;
    },
    async closeDrawer(type) {
      if (type == 1) {
        this.$emit('close');
      } else {
        this.$refs.from.SUBMIT_FORM();
        if (this.saveData) {
          let json = this.saveData;
          json.ruleId = this.formData.ruleId;
          let res = await postSave(json);
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '保存成功',
              type: 'success'
            });
            this.$emit('close', json);
          } else {
            this.saveData = null;
          }
        }
      }
    }
  }
};
</script>
